// pages/question/index.js
var app = getApp();
Page({
  data:{
    tabs: ["问题列表", "我的博问"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    questions:[],
    myQuestions:[],
    userInfo: {}
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
        var that = this;
        app.getUserInfo(function (userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            });
        });
    getList(that);
    getOwnerList(that);
  },
  tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
  },
  onReady:function(){
    // 页面渲染完成
  },
    onPullDownRefresh: function () {
        var that = this;
        wx.showNavigationBarLoading();
        pageIndex = 1;
        this.setData({
            questions: []
        });
        getList(that);
        wx.stopPullDownRefresh;
        wx.hideNavigationBarLoading();
    },

    onReachBottom: function () {
        var that = this;
        getList(that);
    },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
});

var pageIndex = 1;
var pageSize = 10;
//获取首页问题列表
function getList(that) {
    wx.request({
        url: 'https://api.cnblogs.com/api/questions/@sitehome?pageIndex=' + pageIndex + '&pageSize=' + pageSize, //仅为示例，并非真实的接口地址
        method: 'GET',
        data: {
        },
        header: {
            'Authorization': 'Bearer ' + app.accessToken
        },
        success: function (res) {
            var list = that.data.questions;
            for(var i = 0; i < res.data.length; i++){
                list.push(res.data[i]);
            }
            that.setData({
                questions: list
            });
            pageIndex ++;
        },
        fail: function () {
            console.log('获取博问列表失败!');
        }
    });
}

var mpageIndex = 1;
var mpageSize = 10;
//获取自己的提问列表
function getOwnerList(that) {
    wx.request({
        url: 'https://api.cnblogs.com/api/questions/@myquestion?pageIndex=' + mpageIndex + '&pageSize=' + mpageSize, //仅为示例，并非真实的接口地址
        method: 'GET',
        data: {
        },
        header: {
            'Authorization': 'Bearer ' + app.accessToken
        },
        success: function (res) {
            var list = that.data.myQuestions;
            for(var i = 0; i < res.data.length; i++){
                list.push(res.data[i]);
            }
            that.setData({
                myQuestions: list
            });
            mpageIndex ++;
        },
        fail: function () {
            console.log('获取用户博问列表失败!');
        }
    });
}